package SystemeB;

import java.io.PipedReader;
import java.io.PipedWriter;
import java.util.ArrayList;

public class RejectedFilter extends Thread {
	// Declarations

	// Create local pipes to that will connect to upstream filters
	PipedReader InputAcceptedListPipe1 = new PipedReader();
	PipedReader InputFullListPipe2 = new PipedReader();

	ArrayList<String> fullStudentList = new ArrayList<String>();
	ArrayList<String> acceptedStudentList = new ArrayList<String>();

	// Create local pipes to that will connect to downstream filter
	PipedWriter OutputPipe1 = new PipedWriter();

	public RejectedFilter(PipedWriter InputFullListPipe,
			PipedWriter InputAcceptedListPipe, PipedWriter OutputPipe1) {
		try {
			// Connect InputPipes to upstream filters
			this.InputAcceptedListPipe1.connect(InputAcceptedListPipe);
			this.InputFullListPipe2.connect(InputFullListPipe);
			System.out
					.println("RejectedFilter:: connected to upstream filters.");
		} catch (Exception Error) {
			System.out
					.println("RejectedFilter:: Error connecting input pipes.");
		} // try/catch

		try {
			// Connect OutputPipe1 to downstream filter
			this.OutputPipe1 = OutputPipe1;
			System.out
					.println("RejectedFilter:: connected to downstream filter.");
		} catch (Exception Error) {
			System.out
					.println("RejectedFilter:: Error connecting output pipe.");
		} // catch

	} // Constructor

	// This is the method that is called when the thread is started in
	// MainFilter

	public void run() {
		// Declarations
		boolean Done1, Done2; // Flags for reading from each pipe

		// Begin process data from the input pipes
		try {
			// Declarations

			// Need to be an array for easy conversion to string
			char[] CharacterValue1 = new char[1];
			char[] CharacterValue2 = new char[1];

			// Indicate when you are done reading on pipes #1 and #2
			Done1 = false;
			Done2 = false;

			// integer values read from the pipes
			int IntegerCharacter1;
			int IntegerCharacter2;

			// lines of text from input pipes #1 and #2
			String LineOfTextFull = "";
			String LineOfTextAccepted = "";

			// Indicate whether lines of text are ready to be output
			// to downstream filters
			boolean bufferedIntoFullList = false;
			boolean bufferedIntoAcceptedList = false;

			// Main loop for reading data

			while (!Done1 || !Done2) {
				// Read pipe #1 full list of student
				if (!Done1) {
					IntegerCharacter1 = InputFullListPipe2.read();
					CharacterValue1[0] = (char) IntegerCharacter1;

					if (IntegerCharacter1 == -1) // pipe #1 is closed
					{
						Done1 = true;
						System.out
								.println("RejectedFilter:: Input InputAcceptedListPipe1 closed.");

						try {
							InputFullListPipe2.close();
						} catch (Exception Error) {
							System.out
									.println("RejectedFilter:: Error closing input InputAcceptedListPipe1.");
						} // try/catch

					} else {

						if (IntegerCharacter1 == '\n') // end of line
						{
							System.out.println("RejectedFilter:: Received: "
									+ LineOfTextFull + " on input InputAcceptedListPipe1.");
							bufferedIntoFullList = true;
							
							fullStudentList.add(LineOfTextFull);
							LineOfTextFull = "";
						} else {
							LineOfTextFull += new String(CharacterValue1);
						} // if

					} // if ( IntegerCharacter1 == -1 )

				} // if (!Done1)

				// Read pipe #2
				if (!Done2) {
					IntegerCharacter2 = InputAcceptedListPipe1.read();
					CharacterValue2[0] = (char) IntegerCharacter2;

					if (IntegerCharacter2 == -1) // pipe #2 is closed
					{
						Done2 = true;
						System.out
								.println("RejectedFilter:: input InputFullListPipe2 closed.");

						try {
							InputAcceptedListPipe1.close();
						} catch (Exception Error) {
							System.out
									.println("RejectedFilter:: Error closing input InputFullListPipe2.");
						} // try/catch
					} else {
						if (IntegerCharacter2 == '\n') // end of line
						{
							System.out.println("RejectedFilter:: Received: "
									+ LineOfTextAccepted + " on input InputFullListPipe2.");
							bufferedIntoAcceptedList = true;
							
							acceptedStudentList.add(LineOfTextAccepted);
							LineOfTextAccepted = "";
						} else {
							LineOfTextAccepted += new String(CharacterValue2);
						} // if

					} // if ( IntegerCharacter2 == -1 )

				} // if (!Done2)

//				if (bufferedIntoFullList) {
//					bufferedIntoFullList = false;
//
//					try {
//						System.out.println("RejectedFilter:: Received "
//								+ LineOfTextFull + " to output pipe.");
//						LineOfTextFull += "\n";
//						
//
//					} catch (Exception IOError) {
//						System.out.println("RejectedFilter:: Write Error.");
//					} // try/catch
//
//					LineOfTextFull = "";
//
//				} // if ( bufferedIntoFullList )
//
//				if (bufferedIntoAcceptedList) {
//					bufferedIntoAcceptedList = false;
//
//					try {
//						System.out.println("RejectedFilter:: Received "
//								+ LineOfTextAccepted + " to output pipe.");
//						LineOfTextAccepted += "\n";
//						
//					} catch (Exception IOError) {
//						System.out.println("RejectedFilter:: Write Error.");
//					} // try/catch
//
//					LineOfTextAccepted = "";
//
//				} // if (bufferedIntoAcceptedList)

				

				
				
			} // while ( !Done1 || !Done2 )
			


		} // try

		catch (Exception Error) {
			System.out.println("RejectedFilter:: Interrupted.");
			System.out.println("RejectedFilter:: " + Error.toString());
		} // catch

		try {
			
			System.out.println("////////// START FLUSH LISTS ///////////");
			System.out.println("////////// full list ///////////");
			for (String line : fullStudentList) {
				System.out.println(line);
			}
			System.out.println("////////// accepted list ///////////");
			for (String line : acceptedStudentList) {
				System.out.println(line);
			}			
			for (String line : fullStudentList) {
				if (!acceptedStudentList.contains(line)) {
					System.out.println("RejectedFilter:: Sending "
							+ line + " to output pipe.");
					// write line of text to the pipe
					line += "\n";
					OutputPipe1.write(line, 0, line.length());
					// signals end of line
					OutputPipe1.flush();
				}

			}
			
			System.out.println("RejectedFilter:: output pipe closed.");
			OutputPipe1.close();
		} catch (Exception Error) {
			System.out.println("RejectedFilter:: Error closing pipes");
		} // try/catch

	} // run

}
